﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using vITs.Site.Models.DataModels;

namespace vITs.Site.Controllers
{
    public class AccountController : Controller
    {
        [Authorize(Roles = "Administratör")]
        [HttpGet]
        public ActionResult Index(Int32 page = 1)
        {
            var accounts = new VitsDB().Accounts;

            return this.View(accounts);
        }

        [Authorize(Roles = "Administratör")]
        [HttpGet]
        public ActionResult CreateAccount()
        {
            return View();
        }

        [Authorize(Roles = "Administratör")]
        [HttpPost]
        public ActionResult CreateAccount(string accountName)
        {
            VitsDB db = new VitsDB();
            Account a = new Account();
            a.Name = accountName;
            var user = db.Users.SingleOrDefault(u => u.EmailAddress == this.User.Identity.Name);
            a.IssuerId = user.UserId;
            db.Accounts.AddObject(a);

            if (ControlAccount(accountName))
            {
                if (db.SaveChanges() == 1)
                {
                    return this.RedirectToAction("Index");
                }
            }
            else
            {
                ViewBag.AccountCreated = "Det gick inte att skapa konto " + a.Name + ", namnet upptaget";
            }

            return View();


        }

        private bool ControlAccount(string accountName)
        {
            VitsDB db = new VitsDB();

            var accounts = db.Accounts.ToList();

            foreach (var a in accounts)
            {
                if (a.Name == accountName)
                    return false;
            }

            return true;
        }

        [HttpGet]
        [Authorize(Roles="Konsult")]
        public ActionResult AccountAdder()
        {
            this.ViewBag.Accounts = from Account a in new VitsDB().Accounts select a;

            return this.PartialView();
        }

        [HttpGet]
        [Authorize(Roles = "Administratör")]
        public ActionResult Edit(Int32 id)
        {
            var account = new VitsDB().Accounts.SingleOrDefault(a => a.AccountId == id);
            if (account == null)
            {
                return this.RedirectToAction("Index");
            }

            return this.View(account);
        }

        [HttpPost]
        [Authorize(Roles = "Administratör")]
        public ActionResult Edit(Account account)
        {
            if (this.ModelState.IsValid)
            {
                VitsDB db = new VitsDB();
                var acc = db.Accounts.SingleOrDefault(a => a.AccountId == account.AccountId);

                if (acc != null)
                {
                    string name = account.Name.Trim();
                    if (!String.IsNullOrEmpty(name))
                    {
                        acc.Name = name;
                        if (db.SaveChanges() > 0)
                        {
                            return this.RedirectToAction("Index");
                        }
                        else
                        {
                            this.ModelState.AddModelError("", "Ändringarna kunde inte sparas.");
                        }
                    }
                    else
                    {
                        this.ModelState.AddModelError("Name", "Du måste ange ett namn.");
                    }
                }
                else
                {
                    this.ModelState.AddModelError("", "Kontot kunde inte hittas.");
                }
            }

            return this.View(account);
        }

        [HttpGet]
        [Authorize(Roles = "Administratör")]
        public ActionResult Delete(Int32 id)
        {
            VitsDB db = new VitsDB();
            var account = db.Accounts.SingleOrDefault(a => a.AccountId == id);

            if (account != null)
            {
                db.Accounts.DeleteObject(account);
                db.SaveChanges();
            }

            return this.RedirectToAction("Index");
        }
    }
}
